<template>
  <a-modal
    :title="$t('选择业务类型')"
    :visible="visible"
    @ok="handleOk"
    :okText="$t('确定')"
    @cancel="handleCancel"
    :cancelText="$t('取消')"
    width="1200px"
    class="modal_title"
    v-drag
  >
    <a-spin :spinning="confirmLoading">
      <j-form-container :disabled="formDisabled">
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-row>
            <a-col :span="24" style="text-align:right;">
              <a-button type="default" @click="clearAll">{{$t('全清')}}</a-button>
              <a-button type="primary" @click="secAll" style="margin-left:8px">{{$t('全选')}}</a-button>
            </a-col>
            <a-col :span="24">
              <a-form-model-item :label="$t('业务类型')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="typeId">
                <j-search-select-tag
                  :placeholder="$t('请选择业务类型')"
                  :dictOptions="typeOptions"
                  v-model="model.typeId"
                  style="width: 50%"
                  @change="(val)=>selectType(val,1)"
                >
                </j-search-select-tag>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item :label="$t('全部业务类型')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-button
                  v-for="type in typeOptions"
                  :key="type.value"
                  :type="isSelected(type.value) ? 'primary' : 'default'"
                  @click="selectType(type.value,2)"
                  style="margin-left: 10px; min-width: 130px"
                >
                  {{ type.label }}
                </a-button>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </j-form-container>
    </a-spin>
  </a-modal>
</template>

<script>
import { initDictOptions } from '@/components/dict/JDictSelectUtil'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
export default {
  name: 'BusinessTypeModal',
  components: {
    JSearchSelectTag,
  },
  data() {
    return {
      visible: false,
      formDisabled: false,
      model: {
        typeId: '',
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {},
      typeOptions: [],
      selectTypeList: [],
      selectedIds: [],
    }
  },
  created() {
    this.initDictConfig()
  },
  computed: {},
  methods: {
    initDictConfig() {
      //初始化字典
      initDictOptions('bis_order.type').then((res) => {
        if (res.success) {
          this.typeOptions = res.result
        }
      })
    },
    selectType(typeId, arg) {
      let that = this
      if (!that.isSelected(typeId)) {
        that.selectedIds.push(typeId)
        that.selectTypeList = that.typeOptions.filter((item) => that.isSelected(item.value))
      }else if(arg == 2){
        that.selectedIds = that.selectedIds.filter(s=>{return s != typeId})
        that.selectTypeList = that.typeOptions.filter((item) => that.isSelected(item.value))
      }
    },
    // 判断是否选中
    isSelected(typeId) {
      return this.selectedIds.includes(typeId)
    },
    handleCancel() {
      this.visible = false
    },
    handleOk() {
      this.$emit('ok', this.selectTypeList)
      this.visible = false
    },
    edit(typeIds) {
      let that = this
      that.visible = true
      if (typeIds) {
        that.selectedIds = typeIds.toString().split(',')
        that.selectTypeList = that.typeOptions.filter((item) => that.isSelected(item.value))
      } else {
        that.selectedIds = []
        that.selectTypeList = []
      }
      that.$nextTick(()=>{
        that.model = {
          typeId: ''
        }
      })
    },
    clearAll(){
      this.selectedIds = []
      this.selectTypeList = []
    },
    secAll(){
      this.selectedIds = this.typeOptions.map(t=>t.value)
      this.selectTypeList = this.typeOptions.filter((item) => this.isSelected(item.value))
      console.log(this.selectedIds)
    }
  },
}
</script>
<style>
</style>